const db = require('./db.js')
const moment = require('moment');

module.exports = {
	// 用户登录
	UserLogin: async (user_name, user_pwd) => {
		const sql = 'select * from user where user_name = ? and user_pwd = ?'
		return await db.query(sql, [user_name, user_pwd])
	},
	// 用户注册
	UserRegister: async (user_name, user_pwd) => {
		const sql =
			`insert into user(user_name,user_pwd,user_register) values('${user_name}','${user_pwd}','${moment().format('YYYY-MM-DD HH:mm:ss')}')`
		return await db.query(sql)
	},
	// 用户详情
	UserInfo: async (user_id) => {
		const sql = 'select * from user where user_id = ?';
		return await db.query(sql, user_id)
	},
	// 注册校验查询是否已有同名
	UserRegisterCheckName: async (user_name) => {
		const sql = 'select * from user where user_name = ?';
		return await db.query(sql, user_name)
	},
	// 更新用户信息
	UserInfoUpdate: async (user_id, setData) => {
		let sqlData = []
		Object.keys(setData).forEach(key => {
			sqlData.push(`${key} = '${setData[key]}'`)
		})
		const sql = `update user set ${sqlData.join(',')} where user_id = ${user_id}`
		return await db.query(sql)
	},
	// 获取用户列表 -- 模糊查询
	UserListSearch: async (user_id,user_nickname) => {
		const sql = `SELECT * FROM user WHERE user_id != ${user_id} AND user_nickname like '%${user_nickname}%'`
		return await db.query(sql)
	}

}
